
package com.cmc.dao.hibernate;

import java.util.List;

import com.cmc.dao.hibernate.BaseDaoHibernate;
import com.cmc.model.CalendarWorktype;
import com.cmc.dao.CalendarWorktypeDao;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.HibernateCallback;

public class CalendarWorktypeDaoHibernate extends BaseDaoHibernate implements CalendarWorktypeDao {

    /**
     * @see com.cmc.dao.CalendarWorktypeDao#getCalendarWorktypes(com.cmc.model.CalendarWorktype)
     */
    public List getCalendarWorktypes(final CalendarWorktype calendarWorktype) {
        //return getHibernateTemplate().find("from CalendarWorktype");

        /* Remove the line above and uncomment this code block if you want 
           to use Hibernate's Query by Example API.*/
        if (calendarWorktype == null) {
            return getHibernateTemplate().find("from CalendarWorktype");
        } else {
            // filter on properties set in the calendarWorktype
            HibernateCallback callback = new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException {
                    Example ex = Example.create(calendarWorktype).ignoreCase().enableLike(MatchMode.ANYWHERE);
                    return session.createCriteria(CalendarWorktype.class).add(ex).list();
                }
            };
            return (List) getHibernateTemplate().execute(callback);
        }
    }

    /**
     * @see com.cmc.dao.CalendarWorktypeDao#getCalendarWorktype(Long id)
     */
    public CalendarWorktype getCalendarWorktype(final Long id) {
        CalendarWorktype calendarWorktype = (CalendarWorktype) getHibernateTemplate().get(CalendarWorktype.class, id);
        if (calendarWorktype == null) {
            log.warn("uh oh, calendarWorktype with id '" + id + "' not found...");
            throw new ObjectRetrievalFailureException(CalendarWorktype.class, id);
        }

        return calendarWorktype;
    }

    /**
     * @see com.cmc.dao.CalendarWorktypeDao#saveCalendarWorktype(CalendarWorktype calendarWorktype)
     */    
    public void saveCalendarWorktype(final CalendarWorktype calendarWorktype) {
        getHibernateTemplate().saveOrUpdate(calendarWorktype);
    }

    /**
     * @see com.cmc.dao.CalendarWorktypeDao#removeCalendarWorktype(Long id)
     */
    public void removeCalendarWorktype(final Long id) {
        getHibernateTemplate().delete(getCalendarWorktype(id));
    }
    
    public List getCalendarWorktypesByStartDateEndDate(final String startDate,final String endDate) {
    	HibernateCallback callback = new HibernateCallback() 
		{
			public Object doInHibernate(Session session) throws HibernateException 
			{
				Criteria criteria = session.createCriteria(CalendarWorktype.class);

				if (!startDate.equals("")) 
				{
					criteria.add(Expression.ge("calendarDate", startDate ));
				}
				if (!endDate.equals("")) 
				{
					criteria.add(Expression.le("calendarDate", endDate ));
				}
				criteria.addOrder(Order.asc("calendarDate"));
				log.debug("criteria:"+criteria.toString());
				return criteria.list();
			}
		};
		return (List) getHibernateTemplate().execute(callback);    	
    }
}
